package com.mmlc.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.classify.SelectorAdapter;
import com.mmlc.network.filter.GoodsDetailRequestFilter;
import com.mmlc.network.responsebean.GoodsDetailBaseResponseBean;
import com.mmlc.network.responsebean.GoodsDetailResponseBean;
import com.mmlc.widget.MyGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/16.
 */
public class GoodsDetailUtils {
    Context context;
    SharedPreferences sp;

    public GoodsDetailUtils(Context context) {
        this.context = context;
    }

    private void intSP() {
        sp = context.getSharedPreferences("SAVE_SIZE", Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    /**
     * 去重 重新排列
     */
    public static List<GoodsDetailResponseBean.DataBean.SpecBean> reSort(List<GoodsDetailResponseBean.DataBean.SpecBean> specBeen) {
        List<GoodsDetailResponseBean.DataBean.SpecBean> sortedList = new ArrayList<>();
        for (int i = 0; i < specBeen.size(); i++) {
            if (i == 0) {
                sortedList.add(specBeen.get(i));
                continue;
            }
            for (int j = 0; j < sortedList.size(); j++) {
                if (sortedList.get(j).getSpec_value().equals(specBeen.get(i).getSpec_value()))
                    break;
                if (j == sortedList.size() - 1) sortedList.add(specBeen.get(i));
            }
        }
        return sortedList;
    }

    OnSelectedListener onSelectedListener;



    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public interface OnSelectedListener {
        void onBaseDatasChanged(GoodsDetailBaseResponseBean.DataBean baseResponseBean);
    }





    Activity activity;
    LinearLayout ll_content;

    GoodsDetailResponseBean goodsDetail;
    private Map<String, List<GoodsDetailResponseBean.DataBean.SpecBean>> sortedMap;
    List<String> spec_item;
    List<String> choose_list;
    List<List<GoodsDetailResponseBean.DataBean.SpecBean>> spec;
    ArrayList<Integer> selectorIndexs = new ArrayList<>();
    public  int time =0;



    public void reloadDatas(Activity activity, GoodsDetailResponseBean goodsDetail, LinearLayout ll_content,
                            OnSelectedListener onSelectedListener ) {
        this.activity = activity;
        this.goodsDetail = goodsDetail;
        this.ll_content = ll_content;
        this.onSelectedListener = onSelectedListener;
        sortedMap = new HashMap<>();

        if (goodsDetail.getCode() == 0) {
            spec_item = goodsDetail.getData().getSpec_item();
            spec = goodsDetail.getData().getSpec();
            for (int i = 0; i < spec_item.size(); i++) {
                List<GoodsDetailResponseBean.DataBean.SpecBean> sortedList = GoodsDetailUtils.reSort(spec.get(i));
                sortedMap.put(spec_item.get(i), sortedList);

                selectorIndexs.add(-1);

//                for (int j = 0; j < spec.get(i).size(); j++) {
//                    if (!ViewUtils.isEmpty(spec.get(i).get(j).getGoods_id()) && goodsDetail.getData().getId().equals(spec.get(i).get(j).getGoods_id())) {
//                        for (int k = 0; k < sortedList.size(); k++) {
//                            if (spec.get(i).get(j).getSpec_value().equals(sortedList.get(k).getSpec_value())) {
//                                selectorIndexs.add(k);
//                            }
//                        }
//                        break;
//                    }
//                }
            }
            reloadSelector();
        }
    }


    private void reloadSelector() {
        if(time<1) {
            intSP();
        }
        ll_content.removeAllViews();

        for (int i = 0; i < sortedMap.size(); i++) {
            /** 动态添加UI */
            SharedPreferences.Editor edit = sp.edit();
            edit.putInt("count",sortedMap.size());
            edit.commit();
            RelativeLayout ll_selector = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_dialog_buy_selector, null);
            TextView tv_selector = ViewUtils.getView(ll_selector, R.id.tv_selector);
            MyGridView grid_selector = ViewUtils.getView(ll_selector, R.id.grid_selector);

            final List<GoodsDetailResponseBean.DataBean.SpecBean> specList = sortedMap.get(spec_item.get(i));

            tv_selector.setText(spec_item.get(i));
            final SelectorAdapter selectorAdapter = new SelectorAdapter(activity.getApplicationContext(), specList);
            grid_selector.setAdapter(selectorAdapter);
            selectorAdapter.setSelected(selectorIndexs.get(i));

            final int index = i;

            grid_selector.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    String choice = spec.get(index).get(position).getSpec_value();
                    SharedPreferences.Editor et =  sp.edit();
                    et.putString(index+"",choice+"");
                    et.commit();
                    if (selectorIndexs.get(index) == position) return;
                    selectorIndexs.set(index, position);
                    selectorAdapter.setSelected(position);
                    /** 获取选中项的value */
                    String value = specList.get(position).getSpec_value();

                    ArrayList<GoodsDetailResponseBean.DataBean.SpecBean> list = new ArrayList();
                    /** 取出所有选中项 */
                    for (int i = 0; i < spec.get(index).size(); i++) {
                        if (value.equals(spec.get(index).get(i).getSpec_value())) {
                                                                 list.add(spec.get(index).get(i));
                        }
                    }

                    /** 重置其他选项 */
                    for (int i = index + 1; i < spec_item.size(); i++) {
                        ArrayList<GoodsDetailResponseBean.DataBean.SpecBean> otherList = new ArrayList();

                        for (int k = 0; k < list.size(); k++) {
                            List<GoodsDetailResponseBean.DataBean.SpecBean> specBeen = spec.get(i);
                            for (int j = 0; j < specBeen.size(); j++) {
                                if (specBeen.get(j).getGoods_id().equals(list.get(k).getGoods_id())) {
                                    otherList.add(specBeen.get(j));
                                }
                            }
                        }
                        sortedMap.put(spec_item.get(i), GoodsDetailUtils.reSort(otherList));
                        selectorIndexs.set(i, -1);
                    }

                    reloadContent();
                    reloadSelector();
                }
            });
            time++;
            ll_content.addView(ll_selector);
        }
    }

    private void reloadContent() {
        for (int i = 0; i < selectorIndexs.size(); i++) {
            if (selectorIndexs.get(i) == -1) return;
        }

        String product_id = getGoods_id();
        GoodsDetailRequestFilter filter = new GoodsDetailRequestFilter((BaseParentActivity) activity);
        filter.requestBean.goods_id = product_id;
        filter.requestBean.type = "base";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoodsDetailBaseResponseBean>() {
            @Override
            public void onSuccess(GoodsDetailBaseResponseBean result) {
                if (onSelectedListener != null)
                    onSelectedListener.onBaseDatasChanged(result.getData());
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }

    public boolean getSpec(){
        for (int i = 0; i < spec.size(); i++) {
            if (selectorIndexs.get(i) == -1) {
                return false;
            }
        }
        return true;
    }

    public String getGoods_id() {
        String goods_id = "";

        if (spec_item == null || spec_item.size() == 0) {
            return goodsDetail.getData().getId();
        }

        for (int i = 0; i < spec.size(); i++) {
            if (selectorIndexs.get(i) == -1) {
                ToastUtils.showToast("请选择" + spec_item.get(i));
                return null;
            }
        }

        /** 第一组选中数据 */
        GoodsDetailResponseBean.DataBean.SpecBean specBean1 = sortedMap.get(spec_item.get(0)).get(selectorIndexs.get(0));
        if (sortedMap.size() == 1) {
            goods_id = specBean1.getGoods_id();
        } else if (sortedMap.size() > 1) {
            /** 第二组选中数据 */
            GoodsDetailResponseBean.DataBean.SpecBean specBean2 = sortedMap.get(spec_item.get(1)).get(selectorIndexs.get(1));

            /** 第一组选中数据列表 */
            List<GoodsDetailResponseBean.DataBean.SpecBean> list0 = new ArrayList<>();
            for (int i = 0; i < spec.get(0).size(); i++) {
                if (ViewUtils.equals(spec.get(0).get(i).getSpec_value(), specBean1.getSpec_value())) {
                    list0.add(spec.get(0).get(i));
                }
            }
            if (sortedMap.size() == 2)//仅有两组
                for (int i = 0; i < spec.get(1).size(); i++) {
                    if (spec.get(1).get(i).getSpec_value().equals(specBean2.getSpec_value())) {
                        for (int j = 0; j < list0.size(); j++) {
                            if (list0.get(j).getGoods_id().equals(spec.get(1).get(i).getGoods_id())) {
                                goods_id = list0.get(j).getGoods_id();
                            }
                        }
                    }
                }
            else if (sortedMap.size() > 2) {
                /** 第三组选中数据 */
                GoodsDetailResponseBean.DataBean.SpecBean specBean3 = sortedMap.get(spec_item.get(2)).get(selectorIndexs.get(1));

                /** 第二组选中数据列表 */
                List<GoodsDetailResponseBean.DataBean.SpecBean> list1 = new ArrayList<>();
                for (int i = 0; i < spec.get(1).size(); i++) {
                    if (spec.get(1).get(i).getSpec_value().equals(specBean2.getSpec_value())) {
                        list1.add(spec.get(1).get(i));
                    }
                }
                if (sortedMap.size() == 3) {
                    for (int i = 0; i < spec.get(2).size(); i++) {
                        if (spec.get(2).get(i).getSpec_value().equals(specBean3.getSpec_value())) {
                            for (int j = 0; j < list0.size(); j++) {
                                if (spec.get(2).get(i).getGoods_id().equals(list0.get(j).getGoods_id())) {
                                    for (int k = 0; k < list1.size(); k++) {
                                        if (spec.get(2).get(i).getGoods_id().equals(list1.get(k).getGoods_id())) {
                                            goods_id = spec.get(2).get(i).getGoods_id();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return goods_id;
    }
}
